package com.ljkj.blockpay.ui.fragment;

import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.os.CountDownTimer;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ljkj.blockpay.R;
import com.ljkj.blockpay.basedata.BaseFragment;
import com.ljkj.blockpay.model.CommonModel;
import com.ljkj.blockpay.presenter.RegisterPresenter;
import com.ljkj.blockpay.presenter.inter.RegisterInter;
import com.ljkj.blockpay.util.ToggleToActivity;
import com.ljkj.blockpay.widget.ClickListener;
import com.ljkj.blockpay.widget.CommonDialog;
import com.luck.picture.lib.tools.ToastUtils;

import java.util.List;


public class EmailRegisterFragment extends BaseFragment<com.ljkj.blockpay.databinding.FragmentEmailRegisterBinding, RegisterPresenter> implements RegisterInter.RegisterInterView,ClickListener {

    private CommonDialog useragreementDialog;
    private CommonDialog privacypolicyDialog;
    private CommonDialog authCodeDialog;
    private TextView sendcode;
    private String password;
    /**
     * 发送验证码倒计时时间  60秒
     */
    private static final long Second = 1000;
    private static final long mTime = 60 * Second;
    private CountDownTimer mTimer;
    private View codeinflate;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    protected void initData() {
        super.initData();
        binding.setOnClick(this::onClick);
        codeinflate = LayoutInflater.from(getActivity()).inflate(R.layout.authcode_layout, null, false);
        sendcode = codeinflate.findViewById(R.id.tv_sendcode);
        binding.etPhone.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        binding.etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                password = editable.toString().trim();
                String character = "^.{8,20}$";
                String number = "^(?=.*[a-z])(?=.*[A-Z]).{1,16}$";
                String specialcharacter = "^(?=.*?[#?!@$%^&*-])$";

                if (password.matches(character)|| password.matches(number) || password.matches(specialcharacter)){
                    binding.ivChars.setImageResource(R.mipmap.icon_checked);
                    binding.ivNumber.setImageResource(R.mipmap.icon_checked);
                    binding.ivSpecialChar.setImageResource(R.mipmap.icon_checked);
                } else if (!password.matches(character) || !password.matches(number) || !password.matches(specialcharacter)) {
                    binding.ivChars.setImageResource(R.mipmap.icon_error);
                    binding.ivNumber.setImageResource(R.mipmap.icon_error);
                    binding.ivSpecialChar.setImageResource(R.mipmap.icon_error);
                }

            }
        });
    }

    @Override
    protected int initContentView() {
        return R.layout.fragment_email_register;
    }

    @Override
    protected RegisterPresenter createPresenter() {
        return new RegisterPresenter(getActivity(),this);
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.tbn_eye:
                if (binding.tbnEye.isChecked()==false){
                    binding.etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    binding.etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                break;
            case R.id.rbn_isagree:
                if (binding.rbnIsagree.isChecked()){

                }else {

                }
                break;
            case R.id.tv_useragreement:
                useragreementDialog = CommonDialog.Builder.with(getActivity()).setContentView(R.layout.aboutuser_layout).setOnClickListener(R.id.btn_agree, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        useragreementDialog.dismiss();
                    }
                }).setOnClickListener(R.id.iv_close, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        useragreementDialog.dismiss();
                    }
                }).forGravity(Gravity.BOTTOM)
                        .fullWidth()
                        .formBottom(true)
                        .setCancelable(false)
                        .setOnKeyListener((dialog, keyCode, event) -> false)
                        .create();
                useragreementDialog.show();
                break;
            case R.id.tv_privacypolicy:
                privacypolicyDialog = CommonDialog.Builder.with(getActivity()).setContentView(R.layout.aboutuser_layout).setOnClickListener(R.id.btn_agree, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        privacypolicyDialog.dismiss();
                    }
                }).setOnClickListener(R.id.iv_close, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        privacypolicyDialog.dismiss();
                    }
                }).forGravity(Gravity.BOTTOM)
                        .fullWidth()
                        .formBottom(true)
                        .setCancelable(false)
                        .setOnKeyListener((dialog, keyCode, event) -> false)
                        .create();
                privacypolicyDialog.show();
                break;
            case R.id.btn_login:
//                getPresenter().email = binding.etPhone.getText().toString();
                ToastUtils.s(getActivity(),binding.etPhone.getText().toString());
                Log.d("YUYUYUYU",binding.etPhone.getText().toString());
                authCodeDialog = CommonDialog.Builder.with(getActivity()).setContentView(R.layout.authcode_layout)
                        .setOnClickListener(R.id.iv_close, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                authCodeDialog.dismiss();
                            }
                        })
                        .setOnClickListener(R.id.tv_sendcode, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                String email = binding.etPhone.getText().toString().trim();
                                Log.d("YUYUYUYU",email);
                                if (!binding.etPhone.getText().toString().isEmpty()){
                                    TextView sendcode = authCodeDialog.getView(R.id.tv_sendcode);
                                    getPresenter().getCode("",email,"","100");
                                    if (mTimer == null) {
                                        mTimer = new CountDownTimer(mTime, Second) {
                                            @Override
                                            public void onTick(long millisUntilFinished) {
                                                sendcode.setEnabled(false);
                                                sendcode.setTextColor(ContextCompat.getColor(getActivity(), R.color.color_CFD3DE));
                                                sendcode.setText("重新发送 " + String.valueOf((millisUntilFinished / 1000) + "秒"));
                                            }

                                            @Override
                                            public void onFinish() {
                                                sendcode.setEnabled(true);
                                                sendcode.setTextColor(ContextCompat.getColor(getActivity(), R.color.color_FFFFFF));
                                                sendcode.setText("重新发送");

                                            }
                                        };
                                    }
                                    startTimer();

                                } else {
                                    ToastUtils.s(getActivity(),"邮箱不能为空");
                                }


                            }
                        })
                        .setOnClickListener(R.id.et_authcode, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                            }
                        })
                        .setOnClickListener(R.id.btn_affrim, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                EditText etAuthCode = authCodeDialog.getView(R.id.et_authcode);
                                if (etAuthCode.getText().toString().isEmpty()){
                                    ToastUtils.s(view.getContext(),"邮箱验证码不能为空！");
                                    return;
                                }
                                getPresenter().userRegister(etAuthCode.getText().toString(),"",binding.etPhone.getText().toString(),"",binding.etPassword.getText().toString(),"1");

                            }
                        })
                        .fullWidth().formBottom(true)  .setCancelable(false)
                        .setOnKeyListener((dialog, keyCode, event) -> false).create();
                authCodeDialog.show();
                break;
            case R.id.tv_momentlogin:
                ToggleToActivity.toLoginActivity(getActivity());
                break;

        }
    }

    @Override
    public void getCodeSuccess() {
        setDownTimer();
    }

    @Override
    public void registersuccess() {
        ToggleToActivity.toMainActivity(getActivity());
    }

    @Override
    public void countrySuccess(List<CommonModel> commonModel) {

    }


    /**
     * 启动计时器
     */
    public void startTimer() {
        if (mTimer != null) {
            mTimer.start();
        }
    }

    private void setDownTimer() {
        Toast.makeText(getActivity(),"startTimer",Toast.LENGTH_LONG);

    }
}